home *** CD-ROM | disk | FTP | other *** search
- Path: news.interport.net!usenet
- From: yaron@interport.net (Adi)
- Newsgroups: comp.lang.c++
- Subject: Re: Boolean evaluation operator
- Date: Sun, 10 Mar 1996 05:16:46 GMT
- Organization: Interport Communications Corp.
- Message-ID: <4hte6e$55k@park.interport.net>
- References: <4hqcnk$aoj@natasha.rmii.com>
- NNTP-Posting-Host: yaron.port.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- disch@aartronics.com wrote:
-
- >We have a Boolean class that contains the following operator:
-
- > // Returns 1 if THIS Boolean Object is false, and 0 if THIS Boolean Object
- > // is true.
- > int
- > Boolean::operator!(); // No referenced object; operates on THIS
-
- >When used in the following code fragment, compilation fails with the message
- >"Error testbed.cpp 47: Illegal structure operation in function main()":
-
- > Boolean bTrue = T;
- > Boolean bFalse = F;
- > if (!bFalse)
- > {
- > cout << "bFalse" << endl;
- > }
- > if (bTrue)
- > {
- > cout << "bTrue" << endl;
- > }
-
- >Is there a way to define an evaluation operator so that "if (bTrue)" will
- >compile and execute as expected?
-
- >Any input would be greatly appreciated.
-
- >Bob Dischner
- >Aartronics Corp
-
-
-
- Try something like this :
-
-
- class Boolean {
- public :
- Boolean(unsigned int b = FALSE)
- : value(b) {}
-
- Boolean(const Boolean& b)
- : value(b.value) {}
-
- virtual ~Boolean() {}
-
- operator unsigned int()
- {
- return value;
- }
-
- Boolean& operator !()
- {
- value = !value; return *this;
- }
-
- Boolean& operator = (const Boolean& t)
- {
- value = t.value;
- return *this;
- )
-
- protected :
- unsigned int value;
- };
-
-
-
- NOTE :
-
- The important thing here is the cast operator to unsigned integer.
-
-
-
- Adi Degani
- New-York, NY
-
-
-